﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using umbraco.presentation.nodeFactory;

namespace SocialFront.Provider.Umbraco
{
    public class NodeHelper
    {
        public static List<Node> GetNodesByAliasType(Node node, string aliasName)
        {
            return GetNodesByAliasType(node, aliasName, null);
        }

        public static List<Node> GetNodesByAliasType(Node node, string aliasName, int? getCountOfNodes)
        {
            List<Node> nodes = new List<Node>();
            int i = 0;
            foreach (Node child in node.Children)
            {
                if (child.NodeTypeAlias == aliasName)
                    nodes.Add(child);

                if (child.Children.Count > 0)
                    nodes.AddRange(GetNodesByAliasType(child, aliasName));
                i++;
                if (i == getCountOfNodes)
                    return nodes;
            }

            return nodes;
        }

        /// <summary>
        /// Adds the keys to web config.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        public static void AddKeysToWebConfig(string key, string value)
        {
            Configuration myConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
            myConfig.AppSettings.Settings.Add(new KeyValueConfigurationElement(key, value));
            myConfig.Save();
        }

        /*public static List<Node> LinkNodesToMemberByAliasType(Node node, string nodeAlias,)
        {
            List<Node> nodes = new List<Node>();

            foreach (Node child in node.Children)
            {
                if (child.NodeTypeAlias == nodeAlias)
                    nodes.Add(child);

                if (child.Children.Count > 0)
                    nodes.AddRange(LinkNodesToMemberByAliasType(child));
            }

            return nodes;
        }*/
    }
}
